package net.georel.representation.categorisation.datasource;

import java.util.NoSuchElementException;

import org.geotools.data.DataUtilities;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.geometry.BoundingBox;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;

public class CreateClassifiedFeatureCollection {

	private int Rows, Columns;
	private static String stRelevanceFieldName ="rel_cpl_pgr";
	private static String stSpaceTimeFieldName ="s_spatiotem";
	private static String stDirectionFieldName ="s_direction";
	private static String stClusterFieldName ="s_cluster";
	private static String stTopicalFieldName ="s_topicalit";
	private static String stCoLocationFieldName ="s_colocatio";
	private static int[][] concatenatedArray;
	private int arraysAdded;
	private FeatureCollection<SimpleFeatureType, SimpleFeature> outputFeatCollection = FeatureCollections.newCollection();
	private FeatureCollection<SimpleFeatureType, SimpleFeature> inputFeatCollection = FeatureCollections.newCollection();
	private static String[] columnNames;
	
public void initialiseArrayConcatenation(int Rows, int Columns){
	this.Rows=Rows;
	this.Columns=Columns;
	concatenatedArray = new int[Rows][Columns];
	arraysAdded  = 0;
}

public void addArray(int[] inputArray){
	
	for(int i = 0;i<inputArray.length;i++){
		concatenatedArray[i][arraysAdded]=inputArray[i];
	}	
	arraysAdded+=1;
	System.out.println(arraysAdded);
}

public void addInputFeatureCollection (FeatureCollection<SimpleFeatureType, SimpleFeature> inputFeatColl){
	this.inputFeatCollection=inputFeatColl;
}

public void setColumnNames(String[] inputColumnNames){
	columnNames = new String[inputColumnNames.length];
	
	for(int i = 0;i<columnNames.length;i++){
		columnNames[i]=inputColumnNames[i];
	}
	
}


public void buildOutputFeatureCollection () throws NoSuchElementException, SchemaException{
	FeatureIterator<SimpleFeature> featIt = inputFeatCollection.features();
	for(int i = 0;i<concatenatedArray.length;i++){
		outputFeatCollection.add(createClassifiedFeature(featIt.next(), i));
	}
}

public FeatureCollection getOutputFeatureCollection(){
	return outputFeatCollection;
}




private static SimpleFeature createClassifiedFeature(SimpleFeature feature, int  arrayPosition) throws SchemaException{
	
	
	
	SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder();
	 featureTypeBuilder.setName("categorised");
	 featureTypeBuilder.setCRS(feature.getFeatureType().getCoordinateReferenceSystem());
	 
	 featureTypeBuilder.add("poi_name", String.class);
	 featureTypeBuilder.add("poi_categor", String.class);
	 featureTypeBuilder.add("poi_type", String.class);
	 
	 for(int i = 0;i<columnNames.length;i++){
	 featureTypeBuilder.add(columnNames[i], Integer.class);
	 }
	 
	 featureTypeBuilder.add(stRelevanceFieldName, Double.class);
	 featureTypeBuilder.add(stSpaceTimeFieldName, Double.class);
	 featureTypeBuilder.add(stDirectionFieldName, Double.class);
	 featureTypeBuilder.add(stClusterFieldName, Double.class);
	 featureTypeBuilder.add(stTopicalFieldName, Double.class);
	 featureTypeBuilder.add(stCoLocationFieldName, Double.class);	
	 featureTypeBuilder.add("Location",Point.class);
	 
	 SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureTypeBuilder.buildFeatureType());
	 
	 featureBuilder.add(feature.getAttribute("poi_name"));
	 featureBuilder.add(feature.getAttribute("poi_categor"));
	 featureBuilder.add(feature.getAttribute("poi_type"));
	 
	 for(int i = 0;i<columnNames.length;i++){
		 featureBuilder.add(concatenatedArray[arrayPosition][i]);
	 }
	 
	 featureBuilder.add(feature.getAttribute(stRelevanceFieldName));
	 featureBuilder.add(feature.getAttribute(stSpaceTimeFieldName));
	 featureBuilder.add(feature.getAttribute(stDirectionFieldName));
	 featureBuilder.add(feature.getAttribute(stClusterFieldName));
	 featureBuilder.add(feature.getAttribute(stTopicalFieldName));
	 featureBuilder.add(feature.getAttribute(stCoLocationFieldName));	 
	 featureBuilder.add(feature.getDefaultGeometry());
	 
	 
	 SimpleFeature outputFeature = featureBuilder.buildFeature(null);	 
	 return outputFeature;
}


}
